package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.WikiDo;

import java.time.LocalDate;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-11 13:36
 */
public interface WikiMapper extends BaseMapper<WikiDo> {
    // 分页查询
    default Page<WikiDo> selectPageList(Long current, Long size, String title, LocalDate startDate, LocalDate endDate, Boolean isPublish) {
        if (current <= 0) current = 1L;
        if (size < 0) size = 0L;
        // 创建分页
        Page<WikiDo> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<WikiDo> wrapper = Wrappers.<WikiDo>lambdaQuery()
                .like(StringUtils.isNotBlank(title), WikiDo::getTitle, title)
                .ge(Objects.nonNull(startDate), WikiDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), WikiDo::getCreateTime, endDate)
                .eq(Objects.nonNull(isPublish), WikiDo::getIsPublish, isPublish)
                .orderByDesc(WikiDo::getWeight)
                .orderByDesc(WikiDo::getCreateTime);

        return selectPage(page, wrapper);
    }

    // 查询最大权重的知识库
    default WikiDo selectOneByMaxWeight() {
        return selectOne(Wrappers.<WikiDo>lambdaQuery()
                .orderByDesc(WikiDo::getWeight)
                .last("LIMIT 1"));
    }
}
